package com.konsung.ksdc.manager.shop;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.ProductInfoBean;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.config.MyApplication;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 商城列表Adapter
 * Created by Administrator on 2016/4/25 0025.
 */
public class ProductItemAdapter extends BaseAdapter {

    private List<ProductInfoBean> productInfoBeanList = new ArrayList<>();
    private Context mContext;

    /**
     * 构造方法
     * @param context 上下文
     * @param productInfoBeans 商品详情信息集合
     */
    public ProductItemAdapter(Context context, List<ProductInfoBean>
            productInfoBeans) {
        super();
        productInfoBeanList = productInfoBeans;
        mContext = context;
    }

    @Override
    public int getCount() {
        if (null != productInfoBeanList) {
            return productInfoBeanList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return productInfoBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater)
                    MyApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.item_product, null);
            new ViewHolder(convertView);
        }
        ViewHolder viewHolder = (ViewHolder) convertView.getTag();

        ProductInfoBean productInfoBean = productInfoBeanList.get(position);
        viewHolder.tvTitle.setText(productInfoBean.getProductName());

        // ### 压缩显示 如果使用后面注释的代码直接设置 设置的图片大一点就会卡爆 ###

        String productIconUrl = productInfoBean.getProductIconUrl();
        Picasso.with(MyApplication.getContext())
                .load(StringUtils.appendPicPath(productIconUrl))
                .placeholder(R.drawable.pic_talkbox_sel)
                .error(R.drawable.pic_talkbox_sel)
                .into(viewHolder.ivProduct);

        viewHolder.tvDiscountPrice.getPaint().setFlags(Paint
                .STRIKE_THRU_TEXT_FLAG);
        String marketPrice = productInfoBean.getMemberPrice();
        viewHolder.tvRealPrice.setText(marketPrice);
        if (!TextUtils.isEmpty(marketPrice)) {
            double marketPri = Double.valueOf(marketPrice);
            String s = StringUtils.appendPay(
                    StringUtils.trans2double(marketPri));
            viewHolder.tvRealPrice.setText(s);
        }

        String discountPrice = productInfoBean.getMarketPrice();
        viewHolder.tvDiscountPrice.setText(discountPrice);
        if (!TextUtils.isEmpty(discountPrice)) {
            double discountPri = Double.valueOf(discountPrice);
            String s = StringUtils.appendPay(
                    StringUtils.trans2double(discountPri));
            viewHolder.tvDiscountPrice.setText(s);
        }

        return convertView;
    }

    /**
     * 适配器
     */
    static class ViewHolder {
        @Bind(R.id.iv_product)
        ImageView ivProduct;
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.tv_real_price)
        TextView tvRealPrice;
        @Bind(R.id.tv_discount_price)
        TextView tvDiscountPrice;

        /**
         * 防止复用 设置tag
         * @param view view
         */
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setTag(this);
        }
    }

    /**
     * 设置更换数据
     * @param data 商品详情信息集合
     */
    public void setData(List<ProductInfoBean> data) {
        productInfoBeanList = data;
        notifyDataSetInvalidated();
    }
}





